<template>
  <div class="service-area-test">
    <div class="page-header">
      <h1>服务区域选择测试</h1>
      <p>测试服务发布时的区域选择功能</p>
    </div>

    <el-card class="test-card">
      <template #header>
        <div class="card-header">
          <span>服务区域选择组件测试</span>
        </div>
      </template>

      <el-form :model="testForm" label-width="120px">
        <el-form-item label="服务名称">
          <el-input v-model="testForm.serviceName" placeholder="请输入服务名称" />
        </el-form-item>

        <ServiceAreaSelector
          v-model="testForm.serviceArea"
          @change="handleAreaChange"
        />

        <el-form-item label="选择结果">
          <div class="result-display">
            <div class="result-item">
              <span class="label">服务区域文本：</span>
              <span class="value">{{ testForm.serviceArea || '未选择' }}</span>
            </div>
            <div class="result-item" v-if="areaData">
              <span class="label">区域类型：</span>
              <span class="value">{{ getAreaTypeText(areaData.areaType) }}</span>
            </div>
            <div class="result-item" v-if="areaData && areaData.areaData">
              <span class="label">详细数据：</span>
              <pre class="json-display">{{ JSON.stringify(areaData.areaData, null, 2) }}</pre>
            </div>
          </div>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 功能说明 -->
    <el-card class="info-card">
      <template #header>
        <div class="card-header">
          <span>功能说明</span>
        </div>
      </template>

      <div class="feature-list">
        <div class="feature-item">
          <h3>🌍 全国范围</h3>
          <p>选择全国范围服务，适用于有全国服务能力的商家</p>
        </div>

        <div class="feature-item">
          <h3>🏛️ 省内服务</h3>
          <p>选择特定省份，服务覆盖整个省份范围</p>
        </div>

        <div class="feature-item">
          <h3>🏙️ 市内服务</h3>
          <p>选择特定省份下的多个城市，支持多选</p>
        </div>

        <div class="feature-item">
          <h3>🏘️ 区内服务</h3>
          <p>选择特定城市下的多个区县，精确到区县级别</p>
        </div>

        <div class="feature-item">
          <h3>✏️ 自定义区域</h3>
          <p>自由描述服务区域，适用于特殊服务范围</p>
        </div>
      </div>
    </el-card>

    <!-- 示例数据 -->
    <el-card class="example-card">
      <template #header>
        <div class="card-header">
          <span>示例数据</span>
        </div>
      </template>

      <div class="example-list">
        <div class="example-item" v-for="example in examples" :key="example.type">
          <div class="example-header">
            <span class="example-type">{{ example.type }}</span>
            <el-button size="small" @click="applyExample(example)">应用示例</el-button>
          </div>
          <div class="example-content">
            <div class="example-text">{{ example.text }}</div>
            <div class="example-desc">{{ example.description }}</div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import ServiceAreaSelector from '@/components/ServiceAreaSelector.vue'

// 测试表单数据
const testForm = reactive({
  serviceName: '专业洗衣服务',
  serviceArea: ''
})

// 区域数据
const areaData = ref(null)

// 区域类型文本映射
const areaTypeMap = {
  nationwide: '全国范围',
  province: '省内服务',
  city: '市内服务',
  district: '区内服务',
  custom: '自定义区域'
}

// 示例数据
const examples = [
  {
    type: '全国范围',
    text: '全国范围',
    description: '适用于连锁品牌或有全国服务能力的商家',
    data: { areaType: 'nationwide' }
  },
  {
    type: '省内服务',
    text: '广东省全省',
    description: '覆盖广东省所有城市和区县',
    data: { areaType: 'province', province: '440000' }
  },
  {
    type: '市内服务',
    text: '广州市、深圳市、珠海市',
    description: '覆盖广东省内的多个指定城市',
    data: { areaType: 'city', province: '440000', cities: ['440100', '440300', '440400'] }
  },
  {
    type: '区内服务',
    text: '朝阳区、海淀区、西城区',
    description: '覆盖北京市内的多个指定区县',
    data: { areaType: 'district', province: '110000', city: '110100', districts: ['110105', '110108', '110102'] }
  },
  {
    type: '自定义区域',
    text: '北京市朝阳区、海淀区；上海市浦东新区、黄浦区',
    description: '自由描述的服务区域，适用于跨省市的特殊服务范围',
    data: { areaType: 'custom', custom: '北京市朝阳区、海淀区；上海市浦东新区、黄浦区' }
  }
]

// 方法
const handleAreaChange = (data) => {
  console.log('区域变化:', data)
  areaData.value = data
}

const getAreaTypeText = (type) => {
  return areaTypeMap[type] || type
}

const applyExample = (example) => {
  testForm.serviceArea = example.text
  areaData.value = {
    areaType: example.data.areaType,
    areaText: example.text,
    areaData: example.data
  }
}
</script>

<style scoped>
.service-area-test {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h1 {
  margin: 0 0 8px 0;
  font-size: 24px;
  color: #303133;
}

.page-header p {
  margin: 0;
  color: #606266;
  font-size: 14px;
}

.test-card,
.info-card,
.example-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.result-display {
  background: #f5f7fa;
  padding: 16px;
  border-radius: 4px;
  border: 1px solid #e4e7ed;
}

.result-item {
  margin-bottom: 12px;
}

.result-item:last-child {
  margin-bottom: 0;
}

.result-item .label {
  font-weight: 600;
  color: #606266;
  margin-right: 8px;
}

.result-item .value {
  color: #303133;
}

.json-display {
  background: #f8f9fa;
  padding: 12px;
  border-radius: 4px;
  border: 1px solid #e9ecef;
  font-size: 12px;
  color: #495057;
  margin-top: 8px;
  overflow-x: auto;
}

.feature-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.feature-item {
  padding: 16px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  background: #fafafa;
}

.feature-item h3 {
  margin: 0 0 8px 0;
  font-size: 16px;
  color: #303133;
}

.feature-item p {
  margin: 0;
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
}

.example-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.example-item {
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  overflow: hidden;
}

.example-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: #f5f7fa;
  border-bottom: 1px solid #e4e7ed;
}

.example-type {
  font-weight: 600;
  color: #303133;
}

.example-content {
  padding: 16px;
}

.example-text {
  font-size: 14px;
  color: #303133;
  margin-bottom: 8px;
  font-weight: 500;
}

.example-desc {
  font-size: 12px;
  color: #909399;
  line-height: 1.5;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .service-area-test {
    padding: 10px;
  }
  
  .feature-list {
    grid-template-columns: 1fr;
  }
  
  .example-header {
    flex-direction: column;
    gap: 8px;
    align-items: flex-start;
  }
}
</style>
